package com.mt.course.service.model;

import java.util.List;
import java.util.Map;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.github.pagehelper.PageHelper;
import com.mt.common.PagerInfo;
import com.mt.common.ServiceResult;
import com.mt.course.entity.ExaminationItemEntity;
import com.mt.course.mapper.ExaminationItemEntityMapper;

@Component
public class ExaminationItemServiceModel {
    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(ExaminationItemServiceModel.class);

    @Autowired
    private ExaminationItemEntityMapper examinationItemEntityMapper;

    public ServiceResult<List<ExaminationItemEntity>> selectList(Map<String, Object> params, PagerInfo<?> pagerInfo) {
        Assert.notNull(this.examinationItemEntityMapper, "Property 'examinationItemEntityMapper' is required.");
        ServiceResult<List<ExaminationItemEntity>> result = new ServiceResult<List<ExaminationItemEntity>>();
        try {
            if (pagerInfo != null) {
                PageHelper.startPage(pagerInfo.getPageIndex(), pagerInfo.getPageSize());
            }

            result.setResult(this.examinationItemEntityMapper.selectList(params));
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ExaminationItemServiceModel][selectList]：query selectList occur exception", e);
        }
        return result;
    }

    public ServiceResult<List<ExaminationItemEntity>> selectDailyTestList(Map<String, Object> params,
        PagerInfo<?> pagerInfo) {
        Assert.notNull(this.examinationItemEntityMapper, "Property 'examinationItemEntityMapper' is required.");
        ServiceResult<List<ExaminationItemEntity>> result = new ServiceResult<List<ExaminationItemEntity>>();
        try {
            if (pagerInfo != null) {
                PageHelper.startPage(pagerInfo.getPageIndex(), pagerInfo.getPageSize());
            }

            result.setResult(this.examinationItemEntityMapper.selectDailyTestList(params));
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ExaminationItemServiceModel][selectList]：query selectList occur exception", e);
        }
        return result;
    }

    public ServiceResult<ExaminationItemEntity> selectInfo(Map<String, Object> params) {
        Assert.notNull(this.examinationItemEntityMapper, "Property 'examinationItemEntityMapper' is required.");
        ServiceResult<ExaminationItemEntity> result = new ServiceResult<ExaminationItemEntity>();
        try {
            result.setResult(this.examinationItemEntityMapper.selectByPrimaryKey(params));
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ExaminationItemServiceModel][selectInfo]：query selectInfo occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> selectCount(Map<String, Object> params) {
        Assert.notNull(this.examinationItemEntityMapper, "Property 'examinationItemEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        try {
            result.setResult(this.examinationItemEntityMapper.selectCount(params));
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ExaminationItemServiceModel][selectInfo]：query selectInfo occur exception", e);
        }
        return result;
    }

    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000,
        rollbackFor = Exception.class)
    public ServiceResult<Integer> insert(ExaminationItemEntity examinationItemEntity) {
        Assert.notNull(this.examinationItemEntityMapper, "Property 'examinationItemEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.examinationItemEntityMapper.insert(examinationItemEntity);
            if (save > 0) {
                id = examinationItemEntity.getExaminationItemId();
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ExaminationItemServiceModel][insert]：query insert occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> updateRelate(ExaminationItemEntity examinationItemEntity) {
        Assert.notNull(this.examinationItemEntityMapper, "Property 'examinationItemEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.examinationItemEntityMapper.updateByPrimaryKeySelective(examinationItemEntity);
            if (save > 0) {
                id = examinationItemEntity.getExaminationItemId();
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ExaminationItemServiceModel][updateByPrimaryKeySelective]:query update occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> updateByPrimaryKeySelective(ExaminationItemEntity examinationItemEntity) {
        Assert.notNull(this.examinationItemEntityMapper, "Property 'examinationItemEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.examinationItemEntityMapper.updateByPrimaryKeySelective(examinationItemEntity);
            if (save > 0) {
                id = examinationItemEntity.getExaminationItemId();
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ExaminationItemServiceModel][updateByPrimaryKeySelective]:query update occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> deleteByPrimaryKey(Integer examinationItemId) {
        Assert.notNull(this.examinationItemEntityMapper, "Property 'examinationItemEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        try {
            int save = this.examinationItemEntityMapper.deleteByPrimaryKey(examinationItemId);
            result.setResult(save);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ExaminationItemServiceModel][updateByPrimaryKeySelective]:query update occur exception", e);
        }
        return result;
    }
}
